#import "RegUI.h"
#import "UIColor+Hex.h"
#import "Tools.h"
#import "AppDelegate.h"
#import "Toast+UIView.h"
#import "MBProgressHUD.h"
#import "ConfigData.h"
#import "ASIFormDataRequest.h"
#import "RadioButton.h"
@interface RegUI (){
    NSString *userType;
    NSString *gender;
    NSString *flag;
    NSString *Vcode;
}

@end

@implementation RegUI
@synthesize dic;
-(void)backFn{
    
    [self.navigationController popViewControllerAnimated:NO];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    self.view.backgroundColor=[UIColor whiteColor];
    
    
    
    
    UILabel*  titleLb=[[UILabel alloc] initWithFrame:CGRectMake(120, 0, 80, 44)];
    titleLb.textColor=[UIColor whiteColor];
    titleLb.text=@"会员注册";
    titleLb.textAlignment=NSTextAlignmentCenter;
    titleLb.font=[UIFont boldSystemFontOfSize:HEAD_SIZE];
    titleLb.backgroundColor=[UIColor clearColor];
    self.navigationItem.titleView=nil;
    self.navigationItem.titleView=titleLb;
    
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    CGRect frame_img= CGRectMake(0,(44-19)/2,19,19);
    UIImageView* backView=[[UIImageView alloc] initWithFrame:frame_img];
    backView.image=[UIImage imageNamed:@"back2016"];
    
    CGRect frameBtn= CGRectMake(0, 0,50, 44);
    UIButton* homeBtn= [[UIButton alloc] initWithFrame:frameBtn];
    
    [homeBtn addTarget:self action:@selector(backFn) forControlEvents:UIControlEventTouchUpInside];
    
    [homeBtn addSubview:backView];
    
    
    UIBarButtonItem* barHomeBtn= [[UIBarButtonItem alloc] initWithCustomView:homeBtn];
    
    self.navigationItem.leftBarButtonItem=barHomeBtn;
    //
//    UIImageView*  inputTblView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 300, 84*2)];
//    inputTblView.layer.borderColor=[[UIColor colorWithHexString:@"bfbfbf"] CGColor];
//    
//    inputTblView.layer.masksToBounds = YES; //没这句话它圆不起来
//    inputTblView.layer.cornerRadius =2.5; //设置图片圆角的尺度
//    inputTblView.layer.borderWidth=0.5;
//    [self.view addSubview:inputTblView];
//    //
//    int y=20;
//    
//    UIImageView* line=[[UIImageView alloc] initWithFrame:CGRectMake(10,20+42, 300, 0.5)];
//    line.layer.backgroundColor=[[UIColor colorWithHexString:@"bfbfbf"] CGColor];
//    [self.view addSubview:line];
//    //
//    line=[[UIImageView alloc] initWithFrame:CGRectMake(10,20+42+42, 300, 0.5)];
//    line.layer.backgroundColor=[[UIColor colorWithHexString:@"bfbfbf"] CGColor];
//    [self.view addSubview:line];
//    //
//    line=[[UIImageView alloc] initWithFrame:CGRectMake(10,20+42+42+42, 300, 0.5)];
//    line.layer.backgroundColor=[[UIColor colorWithHexString:@"bfbfbf"] CGColor];
//    [self.view addSubview:line];
    
    
    UIImage* input=nil;
    
    //
    
//    UILabel*  mpLb=[[UILabel alloc] initWithFrame:CGRectMake(20,y+(42-24)/2,50, 24)];
//    mpLb.text=@"手机号";
//    mpLb.font=[UIFont systemFontOfSize:12];
//    mpLb.textColor=[UIColor colorWithHexString:@"#000000"];
//    [self.view addSubview:mpLb];
//    //
//    uTF=[[UITextField alloc] initWithFrame:CGRectMake(90, y+2, 300-64, 39)];
//    uTF.backgroundColor=[UIColor clearColor];
//    uTF.placeholder=@"请输入11位手机号";
//    uTF.font=[UIFont systemFontOfSize:13];
//    uTF.delegate=self;
//    uTF.keyboardType=UIKeyboardTypePhonePad;
//    [self.view addSubview:uTF];
//    
//    
//    
//    
//    UILabel*  pwdLb=[[UILabel alloc] initWithFrame:CGRectMake(20,y+(42-24)/2+42,50, 24)];
//    pwdLb.text=@"密码";
//    pwdLb.font=[UIFont systemFontOfSize:12];
//    pwdLb.textColor=[UIColor colorWithHexString:@"#000000"];
//    [self.view addSubview:pwdLb];
//    //
//    pTF=[[UITextField alloc] initWithFrame:CGRectMake(90, y+42+1.5,300-64, 39)];
//    pTF.backgroundColor=[UIColor clearColor];
//    pTF.placeholder=@"请输入密码";
//    pTF.delegate=self;
//    pTF.font=[UIFont systemFontOfSize:13];
//    pTF.secureTextEntry = YES;
//    [self.view addSubview:pTF];
//    ////
//    UILabel*  pwd2Lb=[[UILabel alloc] initWithFrame:CGRectMake(20,y+(42-24)/2+42+42,50, 24)];
//    pwd2Lb.text=@"确认密码";
//    pwd2Lb.font=[UIFont systemFontOfSize:12];
//    pwd2Lb.textColor=[UIColor colorWithHexString:@"#000000"];
//    [self.view addSubview:pwd2Lb];
//    //
//    p2TF=[[UITextField alloc] initWithFrame:CGRectMake(90, y+42+1.5+42,300-64, 39)];
//    p2TF.backgroundColor=[UIColor clearColor];
//    p2TF.placeholder=@"请输入确认密码";
//    p2TF.delegate=self;
//    p2TF.font=[UIFont systemFontOfSize:13];
//    p2TF.secureTextEntry = YES;
//    [self.view addSubview:p2TF];
//    
//    
//    
//    /////
//    UILabel*  nicknameLb=[[UILabel alloc] initWithFrame:CGRectMake(20,y+(42-24)/2+42+42+42,50, 24)];
//    nicknameLb.text=@"昵称";
//    nicknameLb.font=[UIFont systemFontOfSize:12];
//    nicknameLb.textColor=[UIColor colorWithHexString:@"#000000"];
//    [self.view addSubview:nicknameLb];
//    //
//    nicknameTF=[[UITextField alloc] initWithFrame:CGRectMake(90, y+42+1.5+42+42,300-64, 39)];
//    nicknameTF.backgroundColor=[UIColor clearColor];
//    nicknameTF.placeholder=@"请输入昵称";
//    nicknameTF.delegate=self;
//    nicknameTF.font=[UIFont systemFontOfSize:13];
//    
//    [self.view addSubview:nicknameTF];
//    //
//    
//    
//    //////
//    
//    UIImage* pressImg=[Tools imageWithColor:[UIColor colorWithHexString:@"ff3837"] size:CGSizeMake(300, 35)];
//    
//    UIButton* loginBtn=[[UIButton alloc] initWithFrame:CGRectMake(10,104+20+84, 300, 35)];
//    [loginBtn setBackgroundColor:[UIColor colorWithHexString:@"ff6665"]];
//    [loginBtn setBackgroundImage:pressImg forState:1];
//    loginBtn.layer.borderWidth=0;
//    [loginBtn.layer setCornerRadius:2.5]; //设置矩形四个圆角半径
//    [loginBtn.layer setMasksToBounds:YES];
//    [loginBtn addTarget:self action:@selector(regFn) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:loginBtn];
//    UILabel* loginLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 301, 35)];
//    loginLb.text=@"注册";
//    loginLb.font=[UIFont boldSystemFontOfSize:15];
//    loginLb.textColor=[UIColor whiteColor];
//    loginLb.textAlignment=NSTextAlignmentCenter;
//    loginLb.backgroundColor=[UIColor clearColor];
//    [loginBtn addSubview:loginLb];
    //
    
    
    
    
    [self loadPersonalLayout];
    
    userType = @"1";
    gender = @"2";
    flag = @"1";
    
}

- (void)loadPersonalLayout{
    NSArray *array = @[@"请输入您的手机号码",@"请输入您的密码",@"请确认您的密码",@"请输入验证码"];
    
    //RadioButton *btn1 = (RadioButton *)[self.view viewWithTag:101];
    
    for (int i = 0; i < array.count; i++) {
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 50+(40)*i, SCREEN_WIDTH-80 , 30)];
        bgView.backgroundColor = [UIColor whiteColor];
        bgView.layer.cornerRadius = 15;
        bgView.layer.borderWidth = 1.0f;
        bgView.layer.borderColor = [UIColor colorWithRed:235/255.0 green:93/255.0 blue:94/255.0 alpha:1.0].CGColor;
        bgView.tag = 200+i;
        [self.view addSubview:bgView];
        bgView.center = CGPointMake(SCREEN_WIDTH/2, bgView.center.y);
        
        UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 2.5, SCREEN_WIDTH-80-40, 25)];
        textField.placeholder = [array objectAtIndex:i];
        textField.tag = 500+i;
        //[textField setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"];
        textField.font = [UIFont systemFontOfSize:13];
        [bgView addSubview:textField];
        
        if (i == 0) {
            textField.keyboardType = UIKeyboardTypePhonePad;
        }else if (i == 1 || i == 2){
            textField.secureTextEntry = YES;
        }else if (i == 3){
            textField.keyboardType = UIKeyboardTypeNumberPad;
        }
        
        
        if (i == array.count-1) {
            textField.frame = CGRectMake(20, 2.5, (SCREEN_WIDTH-80)*2/3-30, 25);
            
            UIView *sepView = [[UIView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-80)*2/3, 2.5, 1, 25)];
            sepView.backgroundColor = [UIColor colorWithRed:235/255.0 green:93/255.0 blue:94/255.0 alpha:1.0];
            [bgView addSubview:sepView];
            
            UIButton *yanzhengBtn = [[UIButton alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-80)*2/3, 2.5, (SCREEN_WIDTH-80)*1/3, 25)];
            [yanzhengBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
            yanzhengBtn.titleLabel.textAlignment = NSTextAlignmentRight;
            yanzhengBtn.titleLabel.font = [UIFont systemFontOfSize:13];
            [yanzhengBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [yanzhengBtn setBackgroundColor:[UIColor clearColor]];
            [bgView addSubview:yanzhengBtn];
            yanzhengBtn.tag = 999;
            [yanzhengBtn addTarget:self action:@selector(getVcode) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    RadioButton *btn1 = [[RadioButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/4-10, 210+10, 80, 20)];
    btn1.tag = 101;
    [btn1 setTitle:@"个人" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn1.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [btn1 setImage:[UIImage imageNamed:@"notSelect"] forState:UIControlStateNormal];
    [btn1 setImage:[UIImage imageNamed:@"Selected"] forState:UIControlStateSelected];
    [btn1 setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    [btn1.imageView setContentMode:UIViewContentModeScaleAspectFit];
    btn1.selected = true;
    [btn1 addTarget:self action:@selector(btnClickEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    RadioButton *btn2 = [[RadioButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*3/4-50, 210+10, 80, 20)];
    btn2.tag = 102;
    [btn2 setTitle:@"商家" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn2.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [btn2 setImage:[UIImage imageNamed:@"notSelect"] forState:UIControlStateNormal];
    [btn2 setImage:[UIImage imageNamed:@"Selected"] forState:UIControlStateSelected];
    [btn2 setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    [btn2.imageView setContentMode:UIViewContentModeScaleAspectFit];
    [btn2 addTarget:self action:@selector(btnClickEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    
    btn1.groupButtons = @[btn1,btn2];
    
    
    UIButton *okBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 280+40, SCREEN_WIDTH-80, 30)];
    [okBtn addTarget:self action:@selector(btnClickEvent5) forControlEvents:UIControlEventTouchUpInside];
    okBtn.tag = 401;
    okBtn.backgroundColor = [UIColor colorWithRed:235/255.0 green:93/255.0 blue:94/255.0 alpha:1.0];
    [okBtn setTitle:@"注册" forState:UIControlStateNormal];;
    //[okBtn setTitleColor:[UIColor colorWithRed:235/255.0 green:93/255.0 blue:94/255.0 alpha:1.0] forState:UIControlStateNormal];
    okBtn.layer.cornerRadius = 15;
    [self.view addSubview:okBtn];
    okBtn.center = CGPointMake(SCREEN_WIDTH/2, okBtn.center.y);
    
    [self layoutSexBtn];
}

-(void)getVcode{
    [self.view endEditing:YES];
    UITextField *tf1 = [self.view viewWithTag:500];
    if (![self checkTel:tf1.text]) {
        return;
    }
    int i = arc4random() % 10000 + 10000;
    Vcode = [NSString stringWithFormat:@"%d",i];
    Vcode = [Vcode stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@""];
//    [req addPostValue:tf1.text forKey:@"mobile"];
//    [req addPostValue:[NSString stringWithFormat:@"%@",Vcode] forKey:@"msg"];
//    [req addPostValue:@"1" forKey:@"needstatus"];
    
    NSString* loginURL=[NSString stringWithFormat:@"%@/sms.php?mp=%@&code=%@",HOST,tf1.text,[NSString stringWithFormat:@"%@",Vcode]];
    loginURL=[loginURL stringByAppendingString:@""];
    
    
    
    NSURL* url=[[NSURL alloc] initWithString:loginURL];
    
    
    ASIFormDataRequest *req=[ASIFormDataRequest requestWithURL:url];
    

    
    
    NSLog(@"req:%@",req.url);
    
    [req startSynchronous];
    NSError *error = [req error];
    if (!error) {
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [MBProgressHUD hideHUDForView:app.window animated:YES];
        NSString *response = [req responseString];
        NSLog(@"返回结果..%@",response);
        [self.view makeToast:@"获取验证码成功" duration:1.0 position:@"center"];
        self.dic=[NSMutableDictionary dictionaryWithDictionary:[response JSONValue]];
        NSLog(@"注册结果..%@",self.dic);
        
    }

    
    UIButton *btn = [self.view viewWithTag:999];
    __block int timeout=59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [btn setTitle:@"获取验证码" forState:UIControlStateNormal];
                btn.userInteractionEnabled = YES;
                
                
            });
        }else{
            //            int minutes = timeout / 60;
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [btn setTitle:[NSString stringWithFormat:@"%@秒后重发",strTime] forState:UIControlStateNormal];
                btn.userInteractionEnabled = NO;
                
            });
            timeout--;
            
        }
    });
    dispatch_resume(_timer);
}


//注册
-(void)btnClickEvent5{
    UITextField *tf1 = [self.view viewWithTag:500];
    UITextField *tf2 = [self.view viewWithTag:501];
    UITextField *tf3 = [self.view viewWithTag:502];
    UITextField *tf4 = [self.view viewWithTag:503];
    
    if (![self checkTel:tf1.text]) {
        
    }else if (tf2.text.length == 0){
        [self.view makeToast:@"请输入密码" duration:1.0 position:@"center"];
    }else if (![tf3.text isEqualToString:tf2.text]){
        [self.view makeToast:@"确认密码应与输入密码相同" duration:1.0 position:@"center"];
    }else if (tf4.text.length == 0){
        [self.view makeToast:@"请输入验证码" duration:1.0 position:@"center"];
    }else if (![tf4.text isEqualToString:Vcode]){
        [self.view makeToast:@"验证码不正确" duration:1.0 position:@"center"];
    }
    else{
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [[MBProgressHUD showHUDAddedTo:app.window animated:YES] setLabelText:@"加载中,请稍后..."];
        
        [NSThread detachNewThreadSelector:@selector(doReg) toTarget:self withObject:nil];
    }
}

- (BOOL)checkTel:(NSString *)str
{
    if ([str length] == 0) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"手机号不能为空" message:@"请输入11位有效手机号" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        return NO;
    }
    NSString *regex = @"^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:str];
    if (!isMatch) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的手机号码" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        return NO;
    }
    return YES;
    
}

//个人还是商家
-(void)btnClickEvent:(UIButton *)sender{
    if (sender.tag == 101) {
        [self layoutSexBtn];
        userType = @"1";
    }else{
        userType = @"2";
        RadioButton *btn1 = [[RadioButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/4-50, 210+10+40, 80, 20)];
        btn1.tag = 106;
        [btn1 setTitle:@"酒吧" forState:UIControlStateNormal];
        [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn1.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [btn1 setImage:[UIImage imageNamed:@"notSelect"] forState:UIControlStateNormal];
        [btn1 setImage:[UIImage imageNamed:@"Selected"] forState:UIControlStateSelected];
        [btn1 setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
        [btn1.imageView setContentMode:UIViewContentModeScaleAspectFit];
        btn1.selected = true;
        [btn1 addTarget:self action:@selector(btnClickEvent3:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn1];
        
        RadioButton *btn2 = [[RadioButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-40, 210+10+40, 80, 20)];
        btn2.tag = 107;
        [btn2 setTitle:@"KTV" forState:UIControlStateNormal];
        [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn2.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [btn2 setImage:[UIImage imageNamed:@"notSelect"] forState:UIControlStateNormal];
        [btn2 setImage:[UIImage imageNamed:@"Selected"] forState:UIControlStateSelected];
        [btn2 setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
        [btn2.imageView setContentMode:UIViewContentModeScaleAspectFit];
        [btn2 addTarget:self action:@selector(btnClickEvent3:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn2];
        
        RadioButton *btn3 = [[RadioButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*3/4-20, 210+10+40, 85, 20)];
        btn3.tag = 108;
        [btn3 setTitle:@"会所" forState:UIControlStateNormal];
        [btn3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn3.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [btn3 setImage:[UIImage imageNamed:@"notSelect"] forState:UIControlStateNormal];
        [btn3 setImage:[UIImage imageNamed:@"Selected"] forState:UIControlStateSelected];
        [btn3 setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
        [btn3.imageView setContentMode:UIViewContentModeScaleAspectFit];
        [btn3 addTarget:self action:@selector(btnClickEvent3:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn3];
        
        btn1.groupButtons = @[btn1,btn2,btn3];
        
        for (int i = 104; i < 106; i++) {
            RadioButton *btn_1 = [self.view viewWithTag:i];
            [btn_1 removeFromSuperview];
        }
    }
}

-(void)layoutSexBtn{
    RadioButton *btn1 = [[RadioButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/4-10-5, 210+10+40, 80, 20)];
    btn1.tag = 104;
    [btn1 setTitle:@"女" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn1.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [btn1 setImage:[UIImage imageNamed:@"notSelect"] forState:UIControlStateNormal];
    [btn1 setImage:[UIImage imageNamed:@"Selected"] forState:UIControlStateSelected];
    [btn1 setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    [btn1.imageView setContentMode:UIViewContentModeScaleAspectFit];
    btn1.selected = true;
    [btn1 addTarget:self action:@selector(btnClickEvent2:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    RadioButton *btn2 = [[RadioButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*3/4-50-5, 210+10+40, 80, 20)];
    btn2.tag = 105;
    [btn2 setTitle:@"男" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn2.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [btn2 setImage:[UIImage imageNamed:@"notSelect"] forState:UIControlStateNormal];
    [btn2 setImage:[UIImage imageNamed:@"Selected"] forState:UIControlStateSelected];
    [btn2 setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    [btn2.imageView setContentMode:UIViewContentModeScaleAspectFit];
    [btn2 addTarget:self action:@selector(btnClickEvent2:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    
    btn1.groupButtons = @[btn1,btn2];
    
    for (int i = 106; i < 109; i++) {
        RadioButton *btn_1 = [self.view viewWithTag:i];
        [btn_1 removeFromSuperview];
    }
}

//性别
-(void)btnClickEvent2:(UIButton *)sender{
    if (sender.tag == 104) {
        gender = @"2";
    }else{
        gender = @"1";
    }
}

//商家类型
-(void)btnClickEvent3:(UIButton *)sender{
    if (sender.tag == 106) {
        flag = @"1";
    }else if (sender.tag == 107){
        flag = @"2";
    }else{
        flag = @"3";
    }
}

-(void)regFn{
    
    NSString * u = uTF.text;
    NSString * p =pTF.text;
    //NSLog(@"u=%@,p=%@",u,p);
    
    if ([u isEqualToString:@""]||[p isEqualToString:@""]) {
        
        [self.view makeToast:@"请先输入11位手机号" duration:1.0 position:@"center"];
        
        return;
    }
    else if([nicknameTF.text isEqualToString:@""]){
        [self.view makeToast:@"请先输入昵称" duration:1.0 position:@"center"];
        
        return;
    }
    else if([nicknameTF.text isEqualToString:@""]){
        [self.view makeToast:@"请先输入密码" duration:1.0 position:@"center"];
        
        return;
    }
    
    
    
    else{
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [[MBProgressHUD showHUDAddedTo:app.window animated:YES] setLabelText:@"加载中,请稍后..."];
        
        [NSThread detachNewThreadSelector:@selector(doReg) toTarget:self withObject:nil];
    }
}
-(void)doReg{
    UITextField *tf1 = [self.view viewWithTag:500];
    UITextField *tf2 = [self.view viewWithTag:501];
    UITextField *tf4 = [self.view viewWithTag:503];
    
    NSString * m = tf1.text;
    NSString * p = tf2.text;
    NSString * c = tf4.text;
    
    
    //异步注册账号
    [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:tf1.text
                                                         password:tf1.text
                                                   withCompletion:
     ^(NSString *username, NSString *password, EMError *error) {
         [self hideHud];
         
         if (!error) {
             
             
             NSString* loginURL=HOST;
             loginURL=[loginURL stringByAppendingString:@"index.php/AppLoginUser/regMp"];
             
             
             
             NSURL* url=[[NSURL alloc] initWithString:loginURL];
             
             
             ASIFormDataRequest *req=[ASIFormDataRequest requestWithURL:url];
             
             
             [req addPostValue:m forKey:@"mp"];
             [req addPostValue:p forKey:@"pwd"];
             [req addPostValue:userType forKey:@"userType"];
             [req addPostValue:gender forKey:@"gender"];
             [req addPostValue:flag forKey:@"flag"];
             //[req addPostValue:p forKey:@"pwd"];
             //[req addPostValue:nicknameTF.text forKey:@"nickname"];
             
             
             
             
             
             [req startSynchronous];
             NSError *error = [req error];
             if (!error) {
                 AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
                 [MBProgressHUD hideHUDForView:app.window animated:YES];
                 NSString *response = [req responseString];
                 
                 
                 self.dic=[NSMutableDictionary dictionaryWithDictionary:[response JSONValue]];
                 NSLog(@"注册结果..%@",self.dic);
                 
                 if ([[self.dic objectForKey:@"code"]intValue] == 1) {
                     TTAlertNoTitle(NSLocalizedString(@"register.success", @"Registered successfully, please log in"));
                     [NSThread detachNewThreadSelector:@selector(sleepFn) toTarget:self withObject:nil];
                     [self performSelectorOnMainThread:@selector(closeLoginPage) withObject:nil waitUntilDone:YES];
                 }else{
                     UIAlertView * successTip = [[UIAlertView alloc] initWithTitle:@"信息提示:" message:@"注册失败！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                     [successTip show];
                 }
                 
                 
             }
         }else{
             AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
             [MBProgressHUD hideHUDForView:app.window animated:YES];
             switch (error.errorCode) {
                 case EMErrorServerNotReachable:
                     TTAlertNoTitle(NSLocalizedString(@"error.connectServerFail", @"Connect to the server failed!"));
                     break;
                 case EMErrorServerDuplicatedAccount:
                     TTAlertNoTitle(NSLocalizedString(@"register.repeat", @"You registered user already exists!"));
                     break;
                 case EMErrorServerTimeout:
                     TTAlertNoTitle(NSLocalizedString(@"error.connectServerTimeout", @"Connect to the server timed out!"));
                     break;
                 default:
                     TTAlertNoTitle(NSLocalizedString(@"register.fail", @"Registration failed"));
                     break;
             }
         }
     } onQueue:nil];
    
//    if (![c isEqualToString:Vcode]) {
//        [self.view makeToast:@"验证码错误" duration:1.0 position:@"center"];
//    }
    

    
}
-(void)closeLoginPage{
    int code_=[self.dic[@"code"] intValue];
    NSLog(@"status==%d",code_);
    
    if(code_==7){//用户名或密码失败
        [self.view makeToast:@"手机号已经存在" duration:1.0 position:@"center"];
        
    }
    
    
    else if(code_==1){//登录成功
        NSString * u = uTF.text;
      
        NSString *sid=[self.dic objectForKey:@"sid"];
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setValue:sid forKey:@"sid"];
        [userDefaults setValue:u forKey:@"u"];
        
        [self.view makeToast:@"注册成功" duration:1.0 position:@"center"];
        

        
        
        
        
        
    }
    
}

-(void)sleepFn{
    sleep(1);
    [self performSelectorOnMainThread:@selector(closeRegPage) withObject:nil waitUntilDone:YES];
    
}
-(void)closeRegPage{
    [self.navigationController popToRootViewControllerAnimated:NO];
}











- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - uitextfield协议处理

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    [textField resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
}


@end
